package cn.ococci.dm.Main.Tools;

import android.net.wifi.ScanResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.ococci.dm.Main.Const;
import cn.ococci.dm.Main.Model.DoorModel;
import cn.ococci.dm.Main.Model.UnitModel;


/**
 * Created by cyan on 15/12/29.
 */
public class FilterTool {


    public static boolean filterWiFiDataWithCanRefresh(ArrayList<DoorModel> mainData,List<ScanResult> scanResultsList){

        //获取匹配到的数据
        List<DoorModel> modelList =  FilterTool.retrievalScanWiFiToDoorModel(scanResultsList);

        if(modelList.isEmpty()){
            return false;
        }

        if(!mainData.isEmpty()){

            Map<String,Integer> tmpValue = new HashMap<>();

            for(int i = 0 ;i<mainData.size();i++){

                tmpValue.put(mainData.get(i).getWifiBssid(),i);
            }

//            LogTool.log(LogTool.CYAN,"可添加个数:"+modelList.size() + ", 原先的个数" + mainData.size());
            for(DoorModel dm:modelList){
                if(!tmpValue.containsKey(dm.getWifiBssid())){
                    mainData.add(dm);
                }

            }
        }else{

            mainData.addAll(modelList);

        }

//        LogTool.log(LogTool.CYAN, " 新增后的个数 : " + mainData.size());

        return true;

    }


    /**
     * 检索数据（WiFi SSID = smart）
     * @param resultList:已经确定的数据
     * @return
     */
    public static List<DoorModel> retrievalScanWiFiToDoorModel(List<ScanResult> resultList){

        List<DoorModel> modelList = new ArrayList<>();

        for( ScanResult result :resultList){
            DoorModel doorModel = new DoorModel();
            doorModel.setWifiName(result.SSID.replace(" \"", ""));
            doorModel.setWifiBssid(result.BSSID.replace(" \"", ""));
            doorModel.setDoorMac(doorModel.getWifiBssid());
            modelList.add(doorModel);

        }

        return  modelList;
    }

    public static List<ScanResult> retrievalScanResult(List<ScanResult> resultList){

        List<ScanResult> list = new ArrayList<>();

        for( ScanResult result :resultList){

            if (result.SSID.contains(Const.DOOR_SSID)){

                list.add(result);
            }
        }

        return list;
    }



    /**
     * 匹配出需要连接的WiFi
     * @param resultList
     * @param bssid
     * @return
     */
    public static ScanResult retrievalScanResult(List<ScanResult> resultList,String bssid){

        if (!StringTool.hasData(resultList) || StringTool.isEmpty(bssid)){
            return null;
        }

        ScanResult scanResult = null;

        for( ScanResult result :resultList){

            if (result.BSSID.replace("\"","").equals(bssid)){
                scanResult =  result;
                break;
            }
        }

        return scanResult;

    }


    /**
     *  匹配出对应的数据
     * @param selectedUnitData
     * @param showUnitData
     */
    public static void matchUnitListData( List<UnitModel> selectedUnitData,List<UnitModel> showUnitData){

        Map<Integer,Integer> idMap = new HashMap<>();

        for(UnitModel show : selectedUnitData){
            int id = (int)show.getUnitId();
            idMap.put(id,id);
        }


        for(UnitModel select:showUnitData){

            int tmpId = (int)select.getUnitId();
            if (idMap.containsKey(tmpId)){

                select.isSelected = true;
            }
        }

    }


    public static void addHeaderData(List<DoorModel> mainData){

        DoorModel model = new DoorModel();
        model.setDoorName("大门名称");
        model.setWifiName("WiFi");
        mainData.add(0,model);

    }


    /**
     * 处理当前显示数据和选中数据之间的切换
     * @param showFloorList
     * @param position
     * @param selectedFloorList
     */
    public static void handlerSelectedFloorList(List<UnitModel> showFloorList,int position,List<UnitModel> selectedFloorList){

        UnitModel model = showFloorList.get(position);
        model.isSelected = !model.isSelected;

        if (model.isSelected){
            selectedFloorList.add(model);

        }else {
            for(UnitModel tmp:showFloorList){
                if (model.getUnitId() == tmp.getUnitId()){
                    selectedFloorList.remove(tmp);
                    break;
                }
            }
        }
    }






}
